package com.inossem.warehousestorage_inossem.activity.refund_storage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.refund_storage.extra.RefundStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.refundStorage.RefundStorageBatchListAdapter;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageBatchListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.refundStorage.RefundStorageSelectBatchRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.List;

public class RefundStorageSelectBatchActivity extends BaseActivity {

    private TextView title;

    private String mSerial, mPickingNumber;

    private List<RefundStorageBatchListBean.DataBean.BatchListBean> allBatch;

    private PullToRefreshLayout refresh;

    private RefundStorageBatchListAdapter adapter;

    private PullableListView refundStorageSelectBatchList;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_refund_storage_select_batch);
        initView(RefundStorageSelectBatchActivity.this);
        initData(RefundStorageSelectBatchActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        refundStorageSelectBatchList = (PullableListView) activity.findViewById(R.id.refundStorageSelectBatchList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.refundStorageSelectBatchRefresh);
    }

    private void initData(BaseActivity activity) {
        title.setText("选择批次");
        ActivityUtils.initBackAndFunction(activity, "物料明细", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        refundStorageSelectBatchList.setPullDownFlag(false);
        refundStorageSelectBatchList.setPullUpFlag(false);
        getExtra(activity);
        getData(activity);
    }

    private void getExtra(Activity activity) {
        mSerial = activity.getIntent().getStringExtra(RefundStorageExtra.SERIAL);
        mPickingNumber = activity.getIntent().getStringExtra(RefundStorageExtra.REFUND_STORAGE_PICKING_NUMBER);
    }

    private void addRefundStorageBatchListData(Context context, List<RefundStorageBatchListBean.DataBean.BatchListBean> refundStorageItemList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new RefundStorageBatchListAdapter(context, refundStorageItemList, new RefundStorageSelectBatchListener() {
                @Override
                public void select(RefundStorageBatchListBean.DataBean.BatchListBean batchListBean) {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(RefundStorageExtra.BATCH, batchListBean);
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);
//                    RefundStorageUtils.updateBatchInfo(context, mSerial, batchListBean);
                    finish();
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(BaseActivity context) {
        RefundStorageSelectBatchRequest.request(context, mSerial, mPickingNumber, new GsonBeanListener<RefundStorageBatchListBean>() {
            @Override
            public void onSuccess(RefundStorageBatchListBean bean) {
                allBatch = bean.getData().getBatchList();
                addRefundStorageBatchListData(context, ActivityUtils.getShowData(allBatch, 0), refundStorageSelectBatchList, true);
            }
        });
    }

    public interface RefundStorageSelectBatchListener {
        void select(RefundStorageBatchListBean.DataBean.BatchListBean batchListBean);
    }

}
